//
//  OwnerViewController.m
//  YLProject
//
//  Created by 王跃 on 2018/1/10.
//  Copyright © 2018年 WY. All rights reserved.
//

#import "OwnerViewController.h"
#import "Owner_HomeHeaderView.h"
#import "Owner_HomeModel.h"
#import "Owner_HomeCell.h"
#import "ViewFactory.h"
@interface OwnerViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)Owner_HomeHeaderView *headerView;
@end

@implementation OwnerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的";

    [self setUpUI];
}
-(void)setUpUI{
    self.view.backgroundColor = kHEXCOLOR(@"f4f5f7");
    [self.view addSubview:self.headerView];
    [self.view addSubview:self.tableView];
}
#pragma mark -- UITableViewDelegate / UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    Owner_HomeModel *model = self.dataSource[section];
    return model.cellArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Owner_HomeModel *homeModel = self.dataSource[indexPath.section];
    Owner_HomeCellModel *homeCellModel = homeModel.cellArray[indexPath.row];
    Owner_HomeCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([Owner_HomeCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell initWithModel:homeCellModel];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Owner_HomeModel *homeModel = self.dataSource[indexPath.section];
    Owner_HomeCellModel *homeCellModel = homeModel.cellArray[indexPath.row];
    id<IProductProtocol> product = [[ViewFactory shareInstance] createProduct:homeCellModel.att];
    if ([product isKindOfClass:[UIViewController class]]) {
        BaseViewController *controller = (BaseViewController *)product;
        controller.hidesBottomBarWhenPushed = YES;
        controller.navigationItem.title = homeCellModel.navTitle;
        [controller method];
        [self.navigationController pushViewController:controller animated:YES];
    }
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if ([scrollView isEqual:self.tableView]) {
        if (scrollView.contentOffset.y < 0 ) {
            self.headerView.frame = CGRectMake(0, 0, Screen_Width,313 -scrollView.contentOffset.y );
        }else{
            self.headerView.frame = CGRectMake(0,-  scrollView.contentOffset.y, Screen_Width, 313);
        }
        NSLog(@"%f----%f",scrollView.contentOffset.y,self.headerView.frame.size.height);
    }
}
-(Owner_HomeHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [Owner_HomeHeaderView headerView];
        _headerView.frame = CGRectMake(0, 0, Screen_Width, 313 + 44 + STATUS_BAR_HEIGHT);
    }
    return _headerView;
}
-(UITableView *)tableView{
    if (!_tableView)  {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height - TAB_BAR_HEIGHT- STATUS_BAR_HEIGHT - 44 ) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
        [_tableView registerNib:[UINib nibWithNibName:@"Owner_HomeCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass(Owner_HomeCell.class)];
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = [UIColor clearColor];
        UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 313)];
        headerView.backgroundColor = [UIColor clearColor];
        _tableView.tableHeaderView = headerView;
    }
    return _tableView;
}
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithArray:[Owner_HomeModel modelWithArrayForValue]];
    }
    return _dataSource;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
